package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.view.grid_view.XFlowLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.BaseDataBindingFragment
import com.example.shushiyunsst.R
import com.example.shushiyunsst.databinding.GoodsTypeSelectFragmentBinding
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GoodTypeBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GoodTypeSubBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GoodsDetailBean

class GoodsTypeSelectFragment : BaseDataBindingFragment<GoodsTypeSelectFragmentBinding>(), View.OnClickListener {

    companion object {
        fun newInstance(): GoodsTypeSelectFragment {
            val args = Bundle()
            val fragment = GoodsTypeSelectFragment()
            fragment.arguments = args
            return fragment
        }
    }

    var mViewModel: GoodsNewDetailViewModel? = null

    var mGoodsTypeAdapter: JssBaseQuickAdapter<GoodTypeBean>? = null

    fun setGoodsDetailBean(mGoodsDetailBean: GoodsDetailBean?) {
        mViewModel?.infoDaoLiveData?.postValue(mGoodsDetailBean)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        mViewModel = ViewModelProvider(this).get(GoodsNewDetailViewModel::class.java)
    }

    override fun layoutId(): Int {
        return R.layout.goods_type_select_fragment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinder.lifecycleOwner = this
        viewBinder.onViewClick = this
        viewBinder.vModel = mViewModel

        initRecyclerView()


        mViewModel?.infoDaoLiveData?.observe(viewLifecycleOwner, {
            try {
                addData()

            } catch (e: Exception) {
                e.printStackTrace()
            }

        })
    }

    private fun addData() {
        viewBinder?.goodsPrice?.text = mViewModel?.goodsPriceText()
        viewBinder?.goodsStore?.text = mViewModel?.goodsStockText()
        addListData()
    }

    private fun addListData() {


        if (mGoodsTypeAdapter?.data?.isEmpty() == false) return

        val infoDaoLiveData = mViewModel?.infoDaoLiveData
        val value = infoDaoLiveData?.value
        val mColorBean = GoodTypeBean()
        mColorBean.typeName = "颜色"
        mColorBean.subList = parseData(
            "color",
            value?.goods?.goodsColor ?: ""
        )

        for(e in mColorBean.subList){
            if (value?.goods?.defaultSelectColorLab()==e.name){
                mColorBean.selectedItem=e
                break
            }
        }

        mGoodsTypeAdapter?.addData(mColorBean)

        val mSizeBean = GoodTypeBean()
        mSizeBean.typeName = "尺寸"
        mSizeBean.subList = parseData(
            "size",
            value?.goods?.goodsSize ?: ""
        )

        for(e in mSizeBean.subList){
            if (value?.goods?.defaultSelectSizeLab()==e.name){
                mSizeBean.selectedItem=e
                break
            }
        }

        mGoodsTypeAdapter?.addData(mSizeBean)
    }

    private fun parseData(parseKey: String, jsonStr: String): List<GoodTypeSubBean> {

        val obj = JSON.parseObject(jsonStr)
        val value = obj[parseKey].toString()
        var array = JSON.parseArray(value, GoodTypeSubBean::class.java)
        if (array == null) array = ArrayList<GoodTypeSubBean>()
        return array
    }

    private fun initRecyclerView() {
        viewBinder.mRecyclerView.layoutManager =
            LinearLayoutManager(_mActivity, LinearLayoutManager.VERTICAL, false)
        mGoodsTypeAdapter =
            object : JssBaseQuickAdapter<GoodTypeBean>(R.layout.goods_type_select_list_item_fragment) {
                override fun convert(holder: JssBaseViewHolder?, item: GoodTypeBean?) {
                    super.convert(holder, item)
                    holder?.setText(R.id.mType, item?.typeName)
                    val mGird: XFlowLayout? = holder?.findView22(R.id.mGird)
                    val mCommAdapter = object :
                        CommAdapter<GoodTypeSubBean>(
                            _mActivity,
                            R.layout.goods_type_select_list_sub_item_fragment
                        ) {
                        override fun convert(
                            holder: XQuickViewHolder?,
                            subItem: GoodTypeSubBean?,
                            position: Int
                        ) {
                            holder?.setText(R.id.mRTextView, subItem?.name)
                                ?.setSelect(
                                    R.id.mRTextView,
                                    subItem?.isEquals(item?.selectedItem) == true
                                )
                                ?.setOnClickListener {
                                    item?.selectedItem = subItem
                                    notifyDataSetChanged()
                                }
                        }

                    }
                    mGird?.adapter = mCommAdapter
                    mCommAdapter.clear()
                    mCommAdapter.addAll(item?.subList)
                }
            }

        viewBinder.mRecyclerView.adapter = mGoodsTypeAdapter
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.cut -> {
                val infoDaoLiveData = mViewModel?.infoDaoLiveData
                val value = infoDaoLiveData?.value
                val selectedCount = value?.goods?.selectedCount ?: 1
                value?.goods?.selectedCount =
                    if (selectedCount <= 1) selectedCount else selectedCount - 1
                infoDaoLiveData?.postValue(value)
            }

            R.id.add -> {
                val infoDaoLiveData = mViewModel?.infoDaoLiveData
                val value = infoDaoLiveData?.value
                val selectedCount = value?.goods?.selectedCount ?: 1
                val goodsStock = value?.goods?.goodsStock ?: 0
                value?.goods?.selectedCount =
                    if (selectedCount >= goodsStock) selectedCount else selectedCount + 1
                infoDaoLiveData?.postValue(value)
            }
        }
    }
}